package algorithm.dynamic_programming;

public class ConverToLetterString {

    public static int number(String str){
        if(str == null || str.length() == 0)
            return 0;
        char[] chars = str.toCharArray();
        int i = process(chars, 0);
        return i;
    }

    public static int process(char[] str, int index){
        if(index == str.length){
            return 1;
        }
        if(str[index] == '0'){
            return 0;
        }
        if(str[index] == '1'){
            int res1 = process(str, index + 1);
            int res2 = 0;
            if(index != str.length - 1)
              res2 = process(str, index + 2);
            return res1 + res2;
        }
        if(str[index] == '2'){
            int res1 = process(str, index + 1);
            int res2 = 0;
            if(str[index + 1]<'7' && index != str.length - 1) {
                res2 = process(str, index + 2);
            }
            return res1 + res2;
        }
        int res = process(str, index + 1);
        return res;
    }

    public static int ways2(String str){
        if(str == null || str.length() == 0)
            return 0;
        char[] chars = str.toCharArray();
        int[] index =new int[chars.length + 1];
        index[chars.length] = 1;
        for (int i = index.length - 2; i >= 0; i--) {
            if(chars[i] == '0'){
                index[i] = 0;
            }else
                if(chars[i] == '1'){
                int res1 = index[i + 1];
                int res2 = 0;
                if(i != chars.length - 1)
                    res2 = index[i + 2];
                index[i] = res1 + res2;
                }else
                if(chars[i] == '2'){
                    int res1 = index[i + 1];
                    int res2 = 0;
                    if(chars[i + 1]<'7' && i != chars.length - 1) {
                        res2 = index[i + 2];
                    }
                    index[i] = res1 + res2;
                }else
                    index[i] = index[i + 1];
        }
        return index[0];
    }


    public static void main(String[] args) {
        String s = "11222120147";
        int number = number(s);
        int i = ways2(s);
        System.out.println(number);
        System.out.println(i);
    }
}
